package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class SubInfoServlet
 */
@WebServlet("/SubInfoServlet")
public class SubInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("in sub info servlet");
		HttpSession session = request.getSession();
		boolean logged;
		 
		 try{
			 	logged = (Boolean) session.getAttribute("subloggedIn");	 
		 	}
		 catch(NullPointerException e){
			 logged = false;
			 response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		 }
		 
			 if(logged)
			 {
				 String subUser = (String) session.getAttribute("subUser");
				 String subPassword = (String) session.getAttribute("subPass");
				 String subEmail = (String) session.getAttribute("subEmail");
				 
				 JSONObject subInfo = new JSONObject();

				 try {
					 
					System.out.println("getting show info");
					subInfo.put("SubUserName", subUser);
					subInfo.put("SubPass", subPassword);
					subInfo.put("SubEmail", subEmail);
					
					
					response.getWriter().print(subInfo.toString());
					response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
				}
			 
		 	}
		 	else
		 	{
			 	System.out.println("No one logged in =(");
			 	response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		 	}

	}

}